/*
 * Created by SharpDevelop.
 * User: Carlos
 * Date: 16/06/2009
 * Time: 12:03 a.m.
 */
using System;
using System.Data;
using System.Collections.Generic;
namespace Lambda.Data.Common{
	public struct DBType
	{
		#region Fields
			public Type type;
			public DbType dbType;
			public SqlDbType sqlDbType;		
		#endregion
		#region Properties
			public Type Type {
				get { return type; }
			}			
			public DbType DbType {
				get { return dbType; }
			}		
			public SqlDbType SqlDbType {
				get { return sqlDbType; }
			}		
		#endregion
		#region Constructors
			public DBType(Type Type, DbType DbType, SqlDbType SqlDbType){
				type = Type;
				dbType = DbType;
				sqlDbType = SqlDbType;
			}		
		#endregion
	}
	public class DbTypeConvertor
	{
		#region Fields && Properties
			private List<DBType> dbTypeList;
			protected List<DBType> DbTypeList{
				get{
					if(dbTypeList == null){
						dbTypeList = new List<DBType>();
					}
					return dbTypeList;
				}
			}
		#endregion
		#region Constructors
			public DbTypeConvertor(){
				DBType dbTypeMapEntry = new DBType(typeof(bool), DbType.Boolean, SqlDbType.Bit);
				DbTypeList.Add(dbTypeMapEntry);
				dbTypeMapEntry = new DBType(typeof(byte), DbType.Double, SqlDbType.TinyInt);
				DbTypeList.Add(dbTypeMapEntry);		
				dbTypeMapEntry = new DBType(typeof(byte[]), DbType.Binary, SqlDbType.Image);
				DbTypeList.Add(dbTypeMapEntry);		
				dbTypeMapEntry = new DBType(typeof(DateTime), DbType.DateTime, SqlDbType.DateTime);
				DbTypeList.Add(dbTypeMapEntry);		
				dbTypeMapEntry = new DBType(typeof(Decimal), DbType.Decimal, SqlDbType.Decimal);
				DbTypeList.Add(dbTypeMapEntry);		
				dbTypeMapEntry = new DBType(typeof(double), DbType.Double, SqlDbType.Float);
				DbTypeList.Add(dbTypeMapEntry);		
				dbTypeMapEntry = new DBType(typeof(Guid), DbType.Guid, SqlDbType.UniqueIdentifier);
				DbTypeList.Add(dbTypeMapEntry);		
				dbTypeMapEntry = new DBType(typeof(Int16), DbType.Int16, SqlDbType.SmallInt);
				DbTypeList.Add(dbTypeMapEntry);		
				dbTypeMapEntry = new DBType(typeof(Int32), DbType.Int32, SqlDbType.Int);
				DbTypeList.Add(dbTypeMapEntry);		
				dbTypeMapEntry = new DBType(typeof(Int64), DbType.Int64, SqlDbType.BigInt);
				DbTypeList.Add(dbTypeMapEntry);		
				dbTypeMapEntry = new DBType(typeof(object), DbType.Object, SqlDbType.Variant);
				DbTypeList.Add(dbTypeMapEntry);		
				dbTypeMapEntry = new DBType(typeof(string), DbType.String, SqlDbType.VarChar);
				DbTypeList.Add(dbTypeMapEntry);			
			}
		#endregion
		#region Methods
			#region Public
				public Type ToNetType(SqlDbType type){return Find(type).Type;}
				public Type ToNetType(DbType type){return Find(type).Type;}
				public SqlDbType ToSqlDbType(Type type){return Find(type).SqlDbType;}
				public SqlDbType ToSqlDbType(DbType type){return Find(type).SqlDbType;}
				public DbType ToDbType(SqlDbType type){return Find(type).DbType;}
				public DbType ToDbType(Type type){return Find(type).DbType;}
				public DBType ToNetDBType(SqlDbType type){return Find(type);}
				public DBType ToNetDBType(DbType type){return Find(type);}
				public DBType ToNetDBType(Type type){return Find(type);}
			#endregion
			#region Protected
				protected DBType Find(SqlDbType type){
					return DbTypeList.Find(delegate(DBType item){return (item.SqlDbType == type);});
				}
				protected DBType Find(DbType type){
					return DbTypeList.Find(delegate(DBType item){return (item.DbType == type);});
				}
				protected DBType Find(Type type){
					return DbTypeList.Find(delegate(DBType item){return (item.Type == type);});
				}
			#endregion
		#endregion
	}
}